﻿using System;
using System.ComponentModel;
using System.Drawing;
using Neonode.Forms;
using Microsoft.Win32;

namespace StopWatch
{
    class FrmStopWatch : CustomView
    {
        private int iHour = 0;
        private int iMin = 0;
        private int iSec = 0;
        private int iTenth = 0;
        private int MiliCount = 0;
        private int SecCount = 0;
        private DateTime StartDate;
        private TimeSpan Time = new TimeSpan(0);
        private System.Windows.Forms.Timer tmrStopWatch;
       

        public FrmStopWatch()
        {
            InitializeComponent();
            Utilities.CheckBgColor(); 
        }

        private void InitializeComponent()
        {
            this.tmrStopWatch = new System.Windows.Forms.Timer();
            this.tmrStopWatch.Tick += new EventHandler(this.tmrStopWatch_Tick);
            this.tmrStopWatch.Interval = 100;
            this.tmrStopWatch.Enabled = false;
  
            this.ToolsSweep.Enabled = true;
            this.LeftSweep.Enabled = false;
            this.ToolsSweep.Occurred += new System.ComponentModel.CancelEventHandler(this.ToolsSweep_Occurred);         
            this.LeftSweep.Occurred += new System.ComponentModel.CancelEventHandler(this.LeftSweep_Occurred);
            this.Click += new EventHandler<System.Windows.Forms.MouseEventArgs>(this.frmStopWatch_Click);
            this.Paint += new EventHandler<Neonode.Forms.PaintEventArgs>(this.frmStopWatch_Paint);
            this.Title.Height = 0;
            this.Description.Text = "Tap to start/pause";
        }

        private void LeftSweep_Occurred(object sender, CancelEventArgs e)
        {
            iHour = 0;
            iMin = 0;
            iSec = 0;
            iTenth = 0;
            MiliCount = 0;
            SecCount = 0;
            this.StartDate = DateTime.Now;
            Time = new TimeSpan(0);
            if (tmrStopWatch.Enabled == false)
            {
                LeftSweep.Enabled = false;
                this.Description.Text = "Tap to start/pause";
            }
            this.Refresh();
        }

        private void ToolsSweep_Occurred(object sender, CancelEventArgs e)
        {
            new FrmMenu().ShowDialog();
            Utilities.CheckBgColor();
        }

        private void frmStopWatch_Click(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (!(this.tmrStopWatch.Enabled = !this.tmrStopWatch.Enabled))
            {
                this.Time = this.Time.Add((TimeSpan)(DateTime.Now - this.StartDate));
                LeftSweep.Enabled = true;
                this.Description.Text = "Tap to start/pause\r\nLeftSweep to reset";
                this.Refresh();
            }
            else
            {
                this.StartDate = DateTime.Now;
            }
        }

        private void frmStopWatch_Paint(object sender, Neonode.Forms.PaintEventArgs e)
        {          
            
            Font font = new Font("Subway Ticker", 24f, FontStyle.Regular);
            Graphics graphics = e.Graphics;
            graphics.DrawImage(Utilities.background, 6, 7);
            graphics.DrawString(this.iHour.ToString("0") + ":" + this.iMin.ToString("00") + ":" + this.iSec.ToString("00") + "." + this.iTenth.ToString("0"), font, new SolidBrush(System.Drawing.Color.Black), (float)8, (float)9);
        }

        private void tmrStopWatch_Tick(object sender, EventArgs e)
        {
            TimeSpan span = this.Time.Add((TimeSpan)(DateTime.Now - this.StartDate));

            RegistryKey key = Registry.LocalMachine.OpenSubKey(@"Software\Herbie\StopWatch\");
            if (((int)key.GetValue("KeepAwake")) == 1)
            {
                Utilities.KeepAwake(0x2a);
            }
            if (((int)key.GetValue("FastReset")) == 1)
            {
                LeftSweep.Enabled = true;
                this.Description.Text = "Tap to start/pause\r\nLeftSweep to reset";
            }
            else
            {
                LeftSweep.Enabled = false;
                this.Description.Text = "Tap to start/pause";
            }
            key.Close();

            this.MiliCount++;
            if (this.SecCount != span.Seconds)
            {
                this.MiliCount = 0;
                this.SecCount = span.Seconds;
            }


            this.iHour = span.Hours;
            this.iMin = span.Minutes;
            this.iSec = span.Seconds;
            this.iTenth = this.MiliCount;

            this.Refresh();

            if (this.MiliCount >= 9)
            {
                this.MiliCount = 0;
            }

            if (this.iHour == 9)
            {
                this.tmrStopWatch.Enabled = false;
            }
        }
    }
}
